package com.rxt.controller;


import com.rxt.common.result.Result;
import com.rxt.mapper.OOrdersMapper;
import com.rxt.model.domain.OOrdersDetail;
import com.rxt.model.vo.*;
import com.rxt.service.IOOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Slf4j
@Api(tags = "订单模块")
@RestController
@RequestMapping("/oOrders")
public class OOrdersController {

    @Autowired
    private IOOrdersService ordersService;

    @Autowired
    private OOrdersMapper ordersMapper;


    @ApiOperation("生成订单")
    @PostMapping("/generatedOrder")
    public Result generatedOrder(@RequestBody OrderVo orderVo) {
        System.out.println(orderVo);
        List<String> orderIdList =  ordersService.generatedOrder(orderVo.getAddrId(),orderVo.getOrderDetailVos());
        return Result.ok(orderIdList);
    }

    @ApiOperation("根据订单号查询订单信息")
    @PostMapping("/getOrders")
    public Result getOrders(@RequestBody List<String> orderIds) {
        List<OOrdersDetail> ordersDetails = ordersService.getOrders(orderIds);
        return Result.ok(ordersDetails);
    }

    @ApiOperation("订单结算")
    @PostMapping("/payForOrder")
    public Result payForOrder(@RequestBody PayForVo payForVo) {
        ordersService.payFor(payForVo);
        return Result.ok();
    }

    @ApiOperation("获取我的订单")
    @GetMapping("/getMyOrder/{status}")
    public Result getMyOrder(@PathVariable Integer status) {
        List<MyOrderVo> allOrder = ordersService.getMyOrder(status);
        return Result.ok(allOrder);
    }

    @ApiOperation("确认收货")
    @GetMapping("/confirmReceipt")
    public Result confirmReceipt(Long id) {
        ordersService.confirmReceipt(id);
        return Result.ok();
    }

    @ApiOperation("删除已完成订单")
    @DeleteMapping("/delete")
    public Result delete(Long id) {
        ordersService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("查询所有卖出订单")
    @GetMapping("/getSaleOrder")
    public Result getSaleOrder() {
        List<SaleOrderVo> saleOrderVo = ordersService.getSaleOrder();
        return Result.ok(saleOrderVo);
    }

    @ApiOperation("根据订单号查询订单")
    @GetMapping("/getOrderByID")
    public Result getOrderById(String orderId) {
        SaleOrderVo saleOrderVo = ordersService.getOrderById(orderId);
        return Result.ok(saleOrderVo);
    }

    /**
     * @param orderId 订单号
     * @return
     */
    @ApiOperation("订单发货")
    @GetMapping("/shipments")
    public Result shipments(String orderId) {
        ordersService.changeStatus(orderId);
        return Result.ok();
    }


    @ApiOperation("获取订单的商品信息")
    @GetMapping("/product")
    public Result getProductInfo(Long id) {
        return Result.ok(ordersMapper.getProductByOrderId(id));
    }
}

